package com.v9.main.controller.flow;

import com.v9.common.dto.IdDto;
import com.v9.common.dto.NameQueryDto;
import com.v9.common.dto.TreeNodeDto;
import com.v9.common.exception.BizException;
import com.v9.common.web.AbstractController;
import com.v9.flow.domain.FlowBizType;
import com.v9.flow.service.FlowBizTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lunshi
 */
@RestController
@Tag(name = "工作流业务类型")
@RequestMapping(
        value = {"/fl/flow/biz"},
        method = {RequestMethod.POST}
)
@Slf4j
public class BizTypeController extends AbstractController {

    @Resource
    private FlowBizTypeService flowBizTypeService;

    @RequestMapping({"list"})
    @Operation(summary = "列表")
    public List<TreeNodeDto> list(@RequestBody NameQueryDto dto) {
        List<TreeNodeDto> treeNodeDtos = flowBizTypeService.buildTreeData();
        if(treeNodeDtos==null){
            treeNodeDtos=new ArrayList<>();
        }
        return treeNodeDtos;
    }

    @RequestMapping({"save"})
    @Operation(summary = "修改")
    public void save(@RequestBody BizTypeDto dto) {
        if(dto.getId()==null){
            FlowBizType info=clone(FlowBizType.class,dto);
            flowBizTypeService.insert(info);
            return;
        }
        FlowBizType info = flowBizTypeService.findById(dto.getId());
        if (info == null) {
            throw new BizException("非法操作");
        }
        flowBizTypeService.updateById(clone(FlowBizType.class, dto));
    }

    @RequestMapping({"del"})
    @Operation(summary = "删除")
    public void del(@RequestBody IdDto dto) {
        flowBizTypeService.remove(dto.getId());
    }

    @Data
    public static class BizTypeDto {
        @Schema(description = "id")
        private Long id;
        @Schema(description = "名称")
        private String name;
        @Schema(description = "上级")
        private Long parentId;
        @Schema(description = "排序")
        private Integer seq;
    }
}
